package com.yanqu.road.server.cmd.activity.guagifts;

import com.yanqu.road.entity.rank.cross.CrossUserRank;
import com.yanqu.road.logic.cross.CrossActivityRankListModel;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.GuaGiftsProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityRankMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = CrossProtocol.C_GUAGIFTS_CONCAT_TARGET_RANK_VALUE, desc = "孤寡献礼 拼接榜单值")
public class CrossGuaConcatTargetRankValueCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        GuaGiftsProto.CrossGuaGetTargetRankValueReqMsg reqMsg = GuaGiftsProto.CrossGuaGetTargetRankValueReqMsg.parseFrom(packet.getBytes());
        CrossActivityRankListModel rankListModel = ActivityRankMgr.getCrossActivityNewRankListModel(reqMsg.getActivityId(), reqMsg.getConditionType());

        GuaGiftsProto.GuaGiftsRelationListRespMsg oldClientMsg = reqMsg.getToClientMsg();

        GuaGiftsProto.GuaGiftsRelationListRespMsg.Builder respMsg = GuaGiftsProto.GuaGiftsRelationListRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setActivityId(oldClientMsg.getActivityId());
        respMsg.setGiftType(oldClientMsg.getGiftType());
        respMsg.setTapType(oldClientMsg.getTapType());


        for (GuaGiftsProto.GuaGiftsRelationTemp guaGiftsRelationTemp : oldClientMsg.getRelationListList()) {
            GuaGiftsProto.GuaGiftsRelationTemp.Builder newGuaRelationTemp = guaGiftsRelationTemp.toBuilder();
            long targetUserId = guaGiftsRelationTemp.getTargetUserId();
            long targetServerId = guaGiftsRelationTemp.getTargetServerId();
            if (null != rankListModel) {
                CrossUserRank myRankInfo = rankListModel.getMyRankInfo(targetUserId, targetServerId);
                if (myRankInfo != null) {
                    newGuaRelationTemp.setTotalGiftCount(myRankInfo.getValue().intValue());
                }
            }
            respMsg.addRelationList(newGuaRelationTemp);
        }

        MessageHelper.sendPacket(serverId, packet.getPlayerId(),
                YanQuMessageUtils.buildMessage(GameProtocol.S_GUAGIFTS_CONCAT_TARGET_RANK_VALUE_CROSS_BACK, respMsg));
    }

    @Override
    public void sendData() {

    }
}
